package ge.telasi.tasks.ui.search;

import ge.telasi.tasks.model.Task;
import ge.telasi.tasks.ui.task.TaskDecontationUtils;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

/**
 * Date search.
 *
 * @author dimitri
 */
public class TaskPrioritySearch extends JPanel implements FieldEditor<TaskPriorityField> {
    static final int[] PRIORITIES = { Task.PRIORITY_LOW, Task.PRIORITY_NORMAL, Task.PRIORITY_HIGH };

    private TaskPriorityField proto;
    private List<JToggleButton> buttons = new ArrayList<JToggleButton>();

    public TaskPrioritySearch() {
        initComponents();
    }

    private void initComponents() {
        setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));
        for (int priority : PRIORITIES) {
            JToggleButton button = new JToggleButton();
            button.setText(TaskDecontationUtils.getPriorityName(priority));
            button.setName(String.valueOf(priority));
            add(button);
            buttons.add(button);
        }
    }

    public TaskPriorityField getField() {
        TaskPriorityField field = new TaskPriorityField();
        List<Integer> priorities = new ArrayList<Integer>();
        for (JToggleButton button : buttons) {
            if (button.isSelected()) {
                int priority = Integer.parseInt(button.getName());
                priorities.add(priority);
            }
        }
        field.setPriorities(priorities);
        return field;
    }

    public void setField(TaskPriorityField field) {
        this.proto = field;
    }

}
